package com.paddyi.controller;

import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.qiniu.MyQiniu;
import com.paddyi.model.Document;
import com.paddyi.service.IDocumentService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;

/**
 *  (Document)
 * @author paddy
 * @since  2019-08-05 06:40:35
 */
@RestController
@RequestMapping("document")
public class DocumentController extends BaseController{

	@Reference(version = "${demo.service.version}")
	private IDocumentService documentService;

	/**
	 * 保存
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/add")
	public HttpResult add(@RequestBody Document record) {
		Long userId = getUserId();
		record.setCreateUserId(userId);
		record.setUpdateUserId(userId);
		return HttpResult.ok(documentService.save(record));
	}

	@PostMapping(value="/addImg")
	public HttpResult addImg(@RequestParam("file")MultipartFile file) {
		String date = new Date().getTime()+"";
		String fileName = file.getOriginalFilename();
		String[] fileNames = fileName.split("\\.");
		String type = fileNames[1];
		String name = date+"."+type;
		MyQiniu.uploadMultipartFile(file,name,true);
		return HttpResult.ok("http://qiniu.paddyi.com/"+name);
	}

	/**
     * 修改
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody Document record) {
        record.setUpdateUserId(getUserId());
        record.setUpdateTime(new Date());
    	return HttpResult.ok(documentService.updateById(record));
    }

    /**
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage() {
		return HttpResult.ok(documentService.findAll());
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/findById")
	public HttpResult findById(@RequestParam Long id) {
		return HttpResult.ok(documentService.getById(id));
	}
}
